using System;
using System.Collections.Generic;
using System.Text;
using Safira.Spome.Console.SpomeWebRef;
using System.IO;

namespace Safira.Spome.Console.Workers
{
    /// <summary>
    /// Files Worker
    /// </summary>
    public sealed class Files
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Files"/> class.
        /// </summary>
        private Files()
        { }

        /// <summary>
        /// Uploads the directory files.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="list">The list.</param>
        /// <param name="directory">The directory.</param>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#" )]
        public static bool UploadDirectoryFiles( string url, string list, string directory, string filter )
        {
            #region Validators

            if ( url == null )
                return false;
            if ( list == null )
                return false;
            if ( directory == null )
                return false;
            if ( filter == null )
                filter = "";

            #endregion

            return true;
        }

        /// <summary>
        /// Uploads the file.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="list">The list.</param>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#" )]
        public static bool UploadFile( string url, string list, string filename )
        {
            #region Validators

            if ( url == null )
                return false;
            if ( list == null )
                return false;
            if ( filename == null )
                return false;

            #endregion

            SpomeServices myProxy       = new SpomeServices();
            FileUploadRequest request   = new FileUploadRequest();
            FileUploadResponse response;
            byte[] content;

            #region reading file

            using ( FileStream fs = new FileStream( filename, FileMode.Open ) )
            {
                using ( BinaryReader br = new BinaryReader( fs ) )
                {
                    try
                    {
                        content = br.ReadBytes( (int) br.BaseStream.Length );
                    }
                    catch ( IOException e )
                    {
                        System.Console.WriteLine("Unable to read file, {0}",e.Message);
                        return false;
                    }
                }
            }

            if ( content == null )
            {
                return false;
            }

            if ( content.Length <= 0 )
            {
                return false;
            }

            #endregion

            request.Url         = url;
            request.List        = list;
            request.Filename    = filename.Substring(filename.LastIndexOf("//"));
            request.Content     = content;

            response = myProxy.UploadFile( request );

            return response.Success;
        }
    }
}
